
<template>
  <el-row class="min-h-screen bg-indigo-400">
    <el-col :lg="16" class=" flex items-center justify-center flex-col">
      <div class=" font-bold text-5xl text-light-50 mb-4">欢迎光临</div>
      <div class=" text-gray-200 text-sm">此站点为南京网博 Vue3+Vite 网站后台</div>
    </el-col>
    <el-col :lg="8" class=" bg-light-50 flex items-center justify-center flex-col">
      <div class=" font-bold text-3xl text-gray-700">欢迎回来</div>
      <div class="flex items-center justify-center my-5 text-gray-300 space-x-2">
        <span class=" h-[1px] w-16 bg-gray-200"></span>
        <span>账号密码登录</span>
        <span class=" h-[1px] w-16 bg-gray-200"></span>
      </div>
      <el-form :rules="rules" :model="form" class="w-[260px]" ref="formRef">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon class="el-input__icon">
                <el-icon>
                  <User />
                </el-icon>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="请输入密码">
            <template #prefix>
              <el-icon class="el-input__icon">
                <el-icon>
                  <Lock />
                </el-icon>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-button round type="primary" color="#626aef" @click="onSubmit" class="w-[260px]">登 陆</el-button>
      </el-form>
    </el-col>
  </el-row>
</template>
<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { ref,reactive } from 'vue'
import {ElNotification, type FormInstance} from 'element-plus';
import {login} from "../api/user.ts";
// do not use same name with ref
const form = reactive({
  username: '',
  password: ''
})

// 明确指定 formRef 的类型
const formRef = ref<FormInstance | null>(null)

const onSubmit = () => {
  if (formRef.value) {
    formRef.value.validate((result: boolean) => { // 这里 result 通常是布尔类型
      if (!result) {
        // 这里可以添加一些验证失败的提示逻辑
        return;
      }
      login(form.username, form.password)
          // 响应后的回调函数
          .then(result => {
            console.log(result)
            if(result.data!=null){
              ElNotification({
                title: 'Success',
                type: 'success',
              })
            }else{
              ElNotification({
                title: 'Error',
                type: 'error',
              })
            }
          })
    });
  }
}

const rules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 3, max: 5, message: '用户名长度必须为3-5个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' }
  ]
}
</script>

